package com.example.projectcode.controller.Teacher;

import com.example.projectcode.model.Chapter;
import com.example.projectcode.model.College;
import com.example.projectcode.model.Subject;
import com.example.projectcode.model.Testpoint;
import com.example.projectcode.result.Result;
import com.example.projectcode.service.ChapterService;
import com.example.projectcode.service.CollegeService;
import com.example.projectcode.service.SubjectService;
import com.example.projectcode.service.TestPointService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping(value = "/teacher/graduateInfoManage")
@RestController
@Api(tags = "考研信息管理")
public class GraduateInfoManage {

    @Autowired
    private CollegeService collegeService;

    @Autowired
    private SubjectService subjectService;

    @Autowired
    private ChapterService chapterService;

    @Autowired
    private TestPointService testPointService;

    @ApiOperation(value = "查询广东省院校信息")
    @GetMapping("/viewcollegeInfo")
    public Result viewcollegeInfo(){
           return Result.ok(collegeService.selectAllColleges());
    }


    @ApiOperation("增加院校信息")
    @GetMapping(value = "insertCollege/{collegeName}")
    public Result insertCollege(@PathVariable String collegeName){
        int result  = collegeService.insertCollege(collegeName);
        if (result >0){
            return Result.ok().message("添加成功");
        }else {
            return Result.fail().message("添加失败");
        }
    }

    @ApiOperation("根据院校Id删除院校")
    @GetMapping(value = "/deleteCollegeById/{CollegeId}")
    public Result deleteCollege(@PathVariable Integer CollegeId){
        int result = collegeService.deleteCollegeById(CollegeId);
        if (result >0 ){
            return Result.ok().message("删除成功");
        }else {
            return Result.fail().message("删除失败");
        }
    }


    @ApiOperation("根据院校id修改信息")
    @PostMapping("updateCollege")
    public Result updateCollege(@RequestBody College college){
       int result = collegeService.updateCollege(college);
       if(result >0){
           return Result.ok().message("更新成功");
       }else {
           return Result.fail().message("更新失败");
       }
    }

  /*   --------------------------------------添加和删除科目-----------------------------*/
    @ApiOperation("教师查看科目信息")
    @GetMapping("/viewSubjects")
    public Result viewSubjects(){
        List<Subject> subjectList = subjectService.selectAllSubject();
        return Result.ok(subjectList);
    }

    @ApiOperation("添加科目")
    @GetMapping("/insertSubject/{subjectName}")
    public Result insertSubject(@PathVariable String subjectName){
        int result = subjectService.insertSubject(subjectName);
        if (result>0){
            return Result.ok().message("添加成功");
        }else {
            return Result.fail().message("添加失败");
        }
    }

    @ApiOperation("删除科目")
    @GetMapping(value = "/deleteSubjectById/{subjectId}")
    public Result deleteSubject(@PathVariable Integer subjectId){
        int result = subjectService.deleteSubjectById(subjectId);

        if (result>0){
            return Result.ok().message("删除成功");
        }else {
            return Result.fail().message("删除失败");
        }
    }



    /*   -------------------------------------- 章节增删改查-----------------------------*/

    @ApiOperation("查看所有章节")
    @GetMapping("/getAllChapter")
    public Result getAllChapter(){
        List<Chapter> chapterList = chapterService.selectAllChapter();
        return Result.ok(chapterList);
    }



    @ApiOperation("添加某科目下的对应章节")
    @GetMapping(value = "insertChapter/{subjectName}/{chapterName}")
    public Result insertChapter(@PathVariable String subjectName,@PathVariable String chapterName){
        Subject subject = new Subject();
        subject = subjectService.selectSubjectBySubName(subjectName);

        Chapter chapter = new Chapter();
        chapter.setSubid(subject.getSubid());
        chapter.setChapname(chapterName);
        int result = chapterService.insertChapter(chapter);

        if (result >0){
            return Result.ok().message("添加成功");
        }else {
            return Result.fail().message("添加失败");
        }

    }

    @ApiOperation("删除某科目下的章节")
    @GetMapping(value = "/deleteChapter/{chapterId}")
    public Result deleteChapter(@PathVariable Integer chapterId){
      int result =  chapterService.deleteChapterById(chapterId);

      if (result>0){
          return Result.ok().message("删除成功");
      }else {
          return Result.fail().message("删除失败");
      }
    }


    /*   -------------------------------------- 考点增删改查-----------------------------*/

    @ApiOperation("查询所有考点")
    @GetMapping(value = "getAlltestPoint")
    public Result getAlltestPoint(){
        List<Testpoint> testpointList = testPointService.selectAllTestpoint();
        return Result.ok(testpointList);
    }

    @ApiOperation("新增某章节下的某考点")
    @GetMapping(value = "inserttestPoint/{chapterName}/{testpointName}")
    public Result inserttestPoint(@PathVariable String chapterName,@PathVariable String testpointName){
        Chapter chapter = new Chapter();
        chapter = chapterService.selectChapterByName(chapterName);

        int result = testPointService.insertTestPoint(chapter,testpointName);

        if (result>0){
            return Result.ok().message("添加成功");

        }else {
            return Result.fail().message("添加失败");
        }

    }



    @ApiOperation("删除考点")
    @GetMapping(value = "/deletetestPooint/{testPointId}")
    public Result deletetestPooint(@PathVariable Integer testPointId){
       int result = testPointService.deleteTestpointById(testPointId);
        if(result>0){
            return Result.ok().message("删除成功");

        }else {
            return Result.fail().message("删除失败");
        }
    }






}
